package com.ls.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.ls.entity.Patient;
import com.ls.entity.ResResult;
import com.ls.entity.UserInfo;
import com.ls.service.Patientervice;
import com.ls.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-05-09
 */
@Controller
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private Patientervice patientervice;




    @RequestMapping("userList")
    public String listUI(){
        return "user/userList";
    }

    //用户查询
    @GetMapping("api/user")
    @ResponseBody
    public ResResult list(){
        List<UserInfo> list = userInfoService.list(null);
        return ResResult.success(list);
    }


    //跳转到用户信息详情页面
    @ApiOperation(value = "携带数据跳转到详情页面")
    @GetMapping("api/user/listUI")
    public String listpage( Integer id,Model model){
        UserInfo user = userInfoService.getById(id);
        model.addAttribute("user",user);
        return "user/list";

    }
//就诊人
    @ApiOperation(value = "就诊人")
    @GetMapping("api/user/patient")
    public String listpatient(Integer id,Model model){
        Patient patient = patientervice.getById(id);
        model.addAttribute("patient",patient);
        return "user/list";
    }

//
    @ApiOperation(value = "携带数据跳转到审核页面")
    @GetMapping("/api/user/shenHeUI")
    public String shenheList(Integer id,Model model){
        UserInfo userInfo = userInfoService.getById(id);
        model.addAttribute("userInfo",userInfo);
        return "user/shenHe";
    }


    @ApiOperation(value = "审核信息")
    @PutMapping("api/user/update")
    @ResponseBody
    public ResResult upadateshe(UserInfo u, HttpServletRequest request){
        boolean b = userInfoService.updateById(u);
        if (b) {
            return ResResult.success();
        }
        return ResResult.error();
    }

//文件上传
    @ApiOperation("文件上传测试")
    @PostMapping("/ui/oss/fileupload")
    @ResponseBody
    public ResResult file(MultipartFile file){
        String url = "";//返回上传的图片路径
        try {
            String bucketName="hello112";                 //blcket名称
            String endpoint = "oss-cn-beijing.aliyuncs.com"; //endpoint地址
            // key, 对应的密钥
            OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5t7erDsAQ7vzbCsETc9u", "rU7tsm7IEaTtXSIFUDUVA4C5VMWuoW");
            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            //生成随机唯一值，使用uuid，添加到文件名称里面
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;
            //调用方法实现上传
            ossClient.putObject(bucketName, fileName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //上传之后文件路径
            // https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
            url = "https://" + bucketName + "." + endpoint + "/" + fileName;
            System.out.println(url);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  ResResult.success(url);
    }




}

